import { Page } from "@/type/proTable"

/**
 * 表格查询方法
 * @param requestApi 请求函数
 * @param searchParams 查询参数
 * @param customPage 自定义分页
 * @returns
 */
export const useTable = (requestApi:Function,searchParams?:any,customPage?:Page) => {
    const page = customPage||reactive<Page>({
        pageSize:20,
        pageNumber:1,
        pageSizes:[20,50,100],
        total:0
    })

    const loading = ref(false)
    const tableData = ref([])

    const handleSearch = () => {
        loading.value = true
        requestApi(Object.assign({},page,searchParams)).then((res:any) => {
            if(res.code == 200){
                loading.value = false
                tableData.value = res.data.data
                page.total = res.data.total
            }
        })
    }

    const handleSizeChange = (val:number) => {
        page.pageSize = val
        handleSearch()
    }

    const handleCurrentChange = (val:number) => {
        page.pageNumber = val
        handleSearch()
    }

    return {
        page,
        loading,
        tableData,
        handleSearch,
        handleSizeChange,
        handleCurrentChange
    }
}